program Project1;

{$APPTYPE CONSOLE}

 uses
  SysUtils;

type
  pelement=^element;
  element=record
    next: pelement;
    k, a: integer;
  end;
procedure print(var heads: pelement);
var
  current: pelement;
  a, k: integer;
begin
  current:=heads;
  while(current<> nil) do
  begin
    a:=current^.a;
    k:=current^.k;
    Write(k, ' ');
    Writeln(a);
    current:=current^.next;
  end;
end;

procedure AddN(var headn: pelement; a, k: Integer);
var
  tmp: pelement;
begin
  tmp:=headn;
  new(headn);
  headn^.next:=tmp;
  headn^.k:=k;
  headn^.a:=a;
end;

procedure AddM(var headm: pelement; a, k: Integer);
var
  tmp: pelement;
begin
  tmp:=headm;
  new(headm);
  headm^.next:=tmp;
  headm^.k:=k;
  headm^.a:=a;
end;

procedure AddSum(var heads: pelement; asu, ksu: Integer);
var
  tmp: pelement;
begin
  tmp:=heads;
  new(heads);
  heads^.next:=tmp;
  heads^.k:=ksu;
  heads^.a:=asu;
end;

procedure writing(var heads: pelement; wr: integer);
var
  current, currentk, tmp: pelement;
  k, ku, a, i, j: Integer;
begin
  current:=heads;
  k:=current^.k;
  a:=current^.a;
  if (a<>0) then
    write(a, ' * x^', k);
  current:=current^.next;
  for i:=1 to wr-2 do
  begin
    a:=current^.a;
    k:=current^.k;
    if (a<>0) then
      write(' + ', a, ' * x^', k);
    current:=current^.next;
  end;
  a:=current^.a;
  k:=current^.k;
  write(' + ', a, ' *x^', k, ' =0');
end;

procedure Multy(var headn, headm: pelement; n, m: Integer);
var
  currentn, currentm, currents, heads, tmp: pelement;
  i, j, asu, ksu, kreal: integer;
  flag, klag: Boolean;
  wr: integer;
begin
  wr:=0;
  heads:=nil;
  currentn:=headn;
  currentm:=headm;
  for i:=1 to n do
  begin
    asu:=currentm^.a*currentn^.a;
    ksu:=currentm^.k+currentn^.k;
    AddSum(heads, asu, ksu);
    currentn:=currentn^.next;
    wr:=wr+1;
  end;
  for i:=1 to m-1 do
  begin
    currentn:=headn;
    currentm:=currentm^.next;
    currents:=heads;
    for j:=1 to n do
    begin
      flag:=false;
      asu:=currentm^.a*currentn^.a;
      ksu:=currentm^.k+currentn^.k;
      kreal:=currents^.k;
      if (ksu=kreal) then
      begin
        asu:=asu+heads^.a;
        currents^.a := asu;
        flag:=True;
        wr:=wr-1;
      end;
      while (currents^.next<>nil) do
      begin
         kreal:=currents^.next^.k;
        if (ksu=kreal) then
        begin
          asu:=asu+currents^.next^.a;
          currents^.a := asu;
          flag:=True;
          wr:=wr-1;
        end;
        currents:=currents^.next;
      end;
      currents:=heads;
      if (flag=False) then
      begin
        klag:=false;
        while (currents^.next<> nil) do
        begin
          if (currents^.k<ksu) and (currents^.next^.k>ksu) then
          begin
            AddSum(currents^.next, asu, ksu);
            klag:=true;
            break;
          end;
          currents:=currents^.next;
        end;
        if (klag=false) then
        begin
          if (ksu<heads^.k) then
            addsum(heads, asu, ksu);
          if (ksu>heads^.k) then
            addsum(currents^.next, asu, ksu);
        end;
      end;
      currentn:=currentn^.next;
      currents:=heads;
      wr:=wr+1;
    end;
  end;
  writing(heads, wr);
end;

var
  fin: TextFile;
  i, a, k, n, m: integer;
  headn, headm: pelement;
begin
  AssignFile(fin, 'input.txt');
  Reset(fin);
  headn:=nil;
  headm:=nil;
  Readln(fin, n);
  for i:=0 to n-1 do
  begin
    readln(fin, k, a);
    AddN(headn, a, k);
  end;
  Readln(fin, m);
  for i:=0 to m-1 do
  begin
    readln(fin, k, a);
    AddM(headm, a, k);
  end;
  Multy(headn, headm, n, m);
  CloseFile(fin);
  Readln;
end.
